package models

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
)

type DBTables struct {
	DbName         string `gorm:"-" json:"-"`
	TableName      string `gorm:"column:TABLE_NAME" json:"tableName"`
	Engine         string `gorm:"column:ENGINE" json:"engine"`
	TableRows      string `gorm:"column:TABLE_ROWS" json:"tableRows"`
	TableCollation string `gorm:"column:TABLE_COLLATION" json:"tableCollation"`
	CreateTime     string `gorm:"column:CREATE_TIME" json:"createTime"`
	UpdateTime     string `gorm:"column:UPDATE_TIME" json:"updateTime"`
	TableComment   string `gorm:"column:TABLE_COMMENT" json:"tableComment"`
}
type dbSqlite struct {
	Name    string `gorm:"column:name" json:"tableName"`
	TblName string `gorm:"column:tblName" json:"tblName"`
}

func (e *DBTables) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]DBTables, int64, error) {
	var doc []DBTables
	table := new(gorm.DB)
	var count int64
	tbName := new(SysTables).TableName()
	if tx.Name() == "sqlite" {
		var dbSqlite []dbSqlite
		table = tx.Table("sqlite_master")
		table = table.Where(fmt.Sprintf("type='table' and name not in (select table_name from %s)  ", tbName))
		if e.TableName != "" {
			table = table.Where("name like  ? ", e.TableName+"%")
		}
		if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&dbSqlite).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
			return nil, 0, err
		}
		return generate(dbSqlite), count, nil
	}

	if tx.Name() == "mysql" {
		table = tx.Table("information_schema.tables")
		table = table.Where(fmt.Sprintf("TABLE_NAME not in (select table_name from %s) ", tbName))
		table = table.Where("table_schema= ? ", e.DbName)

		if e.TableName != "" {
			table = table.Where("TABLE_NAME = ?", e.TableName)
		}
		if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
			return nil, 0, err
		}
	} else {
		return nil, 0, errors.New("目前代码生成器只支持mysql，后续考虑支持其他数据库")
	}

	//table.Count(&count)
	return doc, count, nil
}
func (e *DBTables) Get(tx *gorm.DB) (DBTables, error) {
	var doc DBTables
	tbName := new(SysTables).TableName()
	if tx.Name() == "sqlite" {
		var sqlite dbSqlite
		table := tx.Table("sqlite_master")
		table = table.Where(fmt.Sprintf("type='table' and name not in (select table_name from %s)  ", tbName))
		if e.TableName != "" {
			table = table.Where("name like  ? ", e.TableName+"%")
		}
		if err := table.First(&sqlite).Error; err != nil {
			return doc, err
		}
		tb := DBTables{
			TableName:    sqlite.Name,
			TableComment: sqlite.TblName,
			Engine:       "sqlite",
		}
		return tb, nil
	}
	if tx.Name() == "mysql" {
		table := tx.Table("information_schema.tables")
		table = table.Where("table_schema= ? ", e.DbName)
		if e.TableName == "" {
			return doc, errors.New("table name cannot be empty！")
		}
		table = table.Where("TABLE_NAME = ?", e.TableName)
		if err := table.First(&doc).Error; err != nil {
			return doc, err
		}
	} else {
		return doc, errors.New("目前代码生成器只支持mysql，后续考虑支持其他数据库")
	}
	return doc, nil
}
func generate(sqlite []dbSqlite) []DBTables {
	var doc []DBTables
	for _, v := range sqlite {
		doc = append(doc, DBTables{
			TableName:    v.Name,
			TableComment: v.TblName,
			Engine:       "sqlite",
		})
	}
	return doc
}
